package com.jinlejia.erp.ui.mine.sign;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivitySignManagerBinding;
import com.jinlejia.erp.ui.mine.adapter.SignManagerAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.DateUtil;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 签到管理
 */
public class SignManagerActivity extends AppCompatActivity implements OnDateSetListener {

    private long millis;
    private SignManagerAdapter adapter;
    ArrayList<Map<String, Object>> dataList = new ArrayList<>();

    private ActivitySignManagerBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_sign_manager);
        binding.setEvent(this);
        millis = System.currentTimeMillis();
        binding.date.setText(DateUtil.getDateWeek(millis));
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setHasFixedSize(true);
        binding.recyclerView.setDivider(R.drawable.shape_h_line);
        adapter = new SignManagerAdapter(dataList);
        adapter.changeMode(YfListMode.MODE_LOADING);
        binding.recyclerView.setAdapter(adapter);
        getData();

        adapter.setFieldOutClickListener((pin, id) -> {
            Intent intent = new Intent(SignManagerActivity.this, DetailFieldSignInActivity.class);
            intent.putExtra("id", id);
            intent.putExtra("pin", pin);
            startActivity(intent);
        });
        adapter.setOnItemClickListener((view, o) -> {
            Map<String, Object> map = (Map<String, Object>) o;
            DialogSignState dialogSignState = new DialogSignState(SignManagerActivity.this);
            dialogSignState.show();
            dialogSignState.setData(map);
        });

    }

    private void getData() {

        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        c.setTimeInMillis(millis);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("year", c.get(Calendar.YEAR));
        jsonObject.put("month", c.get(Calendar.MONTH) + 1);
        jsonObject.put("day", c.get(Calendar.DAY_OF_MONTH));
        OkHttpUtils
                .postString()
                .url(ApiConfig.SIGN_MANAGER)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(SignManagerActivity.this, response, adapter)) {
                            JSONArray jsonArray = JSON.parseArray(response.get("data").toString());
                            ArrayList<Map<String, Object>> list = JSON.parseObject(jsonArray.toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            adapter.setData(list);
                        } else {
                            adapter.changeMode(YfListMode.MODE_EMPTY);
                        }

                    }
                });

    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.pre_btn:
                millis = DateUtil.previousDay(millis);
                binding.date.setText(DateUtil.getDateWeek(millis));
                getData();
                break;
            case R.id.date:
                TimePickerDialog calendarPicker = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH_DAY)
                        .setTitleStringId("选择时间")
                        .setCurrentMillseconds(millis)
                        .setThemeColor(getResources().getColor(R.color.yellow))
                        .setWheelItemTextNormalColor(getResources().getColor(R.color.hintColor))
                        .setWheelItemTextSelectorColor(getResources().getColor(R.color.yellow))
                        .setCallBack(this)
                        .build();
                calendarPicker.show(getSupportFragmentManager(), "year_month_date");
                break;
            case R.id.next_btn:
                millis = DateUtil.nextDay(millis);
                binding.date.setText(DateUtil.getDateWeek(millis));
                getData();
                break;
        }
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        millis = millseconds;
        binding.date.setText(DateUtil.getDateWeek(millseconds));
        getData();
    }
}
